/**
 * 
 */
package com.elephantgreen.xml;

import java.util.Vector;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.elephantgreen.common.ChequeDetails;
import com.elephantgreen.common.ClientTransaction;
import com.elephantgreen.common.ElephantgreenCommon;
import com.elephantgreen.util.Util;

/**
 * @author ABC
 *
 */
public class ClientTransactionXml extends ElephantgreenXmlHandler {

	/**
	 * @param fName
	 * @param parentNodeName
	 */
	public ClientTransactionXml(String fName, String parentNodeName) {
		super(fName, parentNodeName);

	}

	/* (non-Javadoc)
	 * @see com.elephantgreen.persistance.ElephantgreenXmlHandler#getItems()
	 */
	@Override
	public Vector<ElephantgreenCommon> getItems() {


		Vector<ElephantgreenCommon> clntTransaction = new Vector<ElephantgreenCommon>();


		for (int i = 0; i < getNodes().getLength(); i++) {

			Node node = getNodes().item(i);
			if (node.getNodeType() == Node.ELEMENT_NODE) {

				Element element = (Element) node;

				ClientTransaction clientTransaction = new ClientTransaction(
						Util.getAttrValue("clientId", element),
						Util.getTagValue("creditBalance", element),
						new Vector<ChequeDetails>()
						);

				NodeList chequeNode =  element.getElementsByTagName("cheque");

				for (int j = 0; j < chequeNode.getLength(); j++) {

					Node nNode = chequeNode.item(j);
					if (nNode.getNodeType() == Node.ELEMENT_NODE) {

						Element eElement = (Element) nNode;

						ChequeDetails chequeDetails = new ChequeDetails(
								Util.getTagValue("chequeNumber", eElement),
								Util.getTagValue("bankName", eElement),
								Util.getTagValue("branchName", eElement),
								Util.getTagValue("dueDate", eElement),
								Util.getTagValue("chequeAmount", eElement),
								Util.getTagValue("chequeStatus", eElement));


						clientTransaction.addCheque(chequeDetails);
					}
				}

				clntTransaction.add(clientTransaction);

			}
		}

		return clntTransaction;
	}

}
